package com.zrgk.service;

import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zrgk.bean.DeptBean;
import com.zrgk.bean.EmpBean;
import com.zrgk.dao.EmpDao;
import com.zrgk.util.PageUtil;
@Service(value="empService")
@Transactional(propagation =Propagation.REQUIRED)
public class EmpServiceImpl implements EmpService{
	@Autowired
	@Qualifier(value="empDao")
	private EmpDao empDao;
	
	public EmpDao getEmpDao() {
		return empDao;
	}

	public void setEmpDao(EmpDao empDao) {
		this.empDao = empDao;
	}


	public void findEmpByPage(PageUtil pageUtil) {
//		Page page = PageHelper.startPage((pageUtil.getNowPage()-1)*pageUtil.getPageSize(),pageUtil.getPageSize());
		EmpBean empBean=null;
		Map<String, String> map = pageUtil.getSearchInfo();
		if(map!=null&&!map.isEmpty()){
			empBean=new EmpBean();
			if(map.get("empName")!=null){
				empBean.setName(map.get("empName"));
			}
			if(map.get("empPhone")!=null){
				empBean.setPhone(map.get("empPhone"));
			}
			if(map.get("empDeptId")!=null){
				DeptBean dept=new DeptBean();
				dept.setId(Integer.parseInt(map.get("empDeptId")));
				empBean.setDept(dept);
			}
		}
		List<EmpBean> emps = empDao.selectEmpByPage(empBean);
		for(EmpBean emp:emps){
			System.out.println(emp);
		}
		pageUtil.setList(emps);
//		pageUtil.setTotalCount(page.getPages());
//		pageUtil.setTotalPage((int)page.getTotal());	
	}

	public EmpBean findEmpById(int id) {
		return empDao.selectEmpById(id);
	}
	
	public void updateEmp(EmpBean emp){
		empDao.updateEmp(emp);
	}

	public void deleteEmp(int id) {
		empDao.deleteEmp(id);
	}

	public void addEmp(EmpBean emp) {
		int id = empDao.insertEmp(emp);
		System.out.println(id);
		emp=empDao.selectEmpById(id); 
	}

	public void updateEmpState(EmpBean emp) {
		empDao.updateEmpState(emp);
	}

	public boolean checkEmpNumber(String number) {
		 List<EmpBean> emps = empDao.checkEmpNumber(number);
		 if(!emps.isEmpty()&&emps!=null){
			  return false;
		 }else{
			 return true;
		 }
	}

	public void authoriseEmp(EmpBean emp) {
		empDao.authoriseEmp(emp);
		
	}

	public void loginEmp(EmpBean emp) {
		emp=empDao.loginEmp(emp);
		
	}

}
